<?php
Yii::import('ext.feed.*');
class NewsController extends Controller
{
	/**
	 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
	 * using two-column layout. See 'protected/views/layouts/column2.php'.
	 */
	public $layout='//layouts/main';

	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
			array('allow',  // allow all users to perform 'index' and 'view' actions
				'actions'=>array('index','view','show', 'rss'),
				'users'=>array('*'),
			),
			array('allow', // allow authenticated user to perform 'create' and 'update' actions
				'actions'=>array('create','update'),
				'users'=>array('@'),
			),
			array('allow', // allow admin user to perform 'admin' and 'delete' actions
				'actions'=>array('admin','delete'),
				'users'=>array('admin'),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}

  public function actionRss($id)
  {
    $host = 'http://'.Yii::app()->request->getServerName();
    $news = News::model()->findAll('member_id = :member_id', array(':member_id' => $id));

    $feed = new EFeed();

    $feed->title= 'Association of Christian Schools International';
    $feed->description = 'RSS Feed for News';

    $feed->addChannelTag('language', 'en-us');
    $feed->addChannelTag('pubDate', date(DATE_RSS, time()));

    foreach($news as $n)
    {
      $item = $feed->createNewItem();
      $item->title = $n->subject;
      $item->link = $host.bu("news/".$n->id);
      $item->date = $n->created_at;
      $item->description = substr($n->content, 0, 300)."...";
      $feed->addItem($item);
      $feed->generateFeed();
    }
  }

	/**
	 * Displays a particular model.
	 * @param integer $id the ID of the model to be displayed
	 */
	public function actionView($id)
	{
		$this->render('view',array(
			'news'=>$this->loadModel($id),
		));
	}

	/**
	 * Lists all models.
	 */
	public function actionIndex()
	{
		//$news = News::model()->findAll('member_id = :member_id order by created_at DESC', array(":member_id" => $_GET['member']));

    if(isset($_GET['member']))
    {
      $member_id = $_GET['member'];
    }
    else
    {
      $member_id = 0;
    }

    $criteria = new CDbCriteria;
    $criteria -> order = 'level DESC, created_at DESC, modified_at DESC';
    $criteria -> condition = "member_id = ".$member_id.' AND published = 1';

    $news = News::model()->findAll($criteria);
    $count = count($news);
    
    //Pagination   
    $pages = new CPagination($count);
    $pages -> pageSize = 10;
    $pages -> applyLimit($criteria);

    $news = News::model()->findAll($criteria);
    
    // Banners
    $criteria = new CDbCriteria;
    $criteria->order = "created_at";
    $criteria->limit = 7;
    $banners = Banners::model()->findAll($criteria);

		$this->render('index',array(
			'news' => $news,
      'pages' => $pages,
      'banners' => $banners,
		));
	}

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer the ID of the model to be loaded
	 */
	public function loadModel($id)
	{
		$model=News::model()->findByPk((int)$id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param CModel the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='news-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
}
